﻿namespace PowerSong.UserInterface
    {
    using System.Linq;
    using System.Windows;
    using PowerSong.SongDatabase;
    using System;

    /// <summary>
    /// Interaction logic for EditLicensesWindow.xaml
    /// </summary>
    public partial class EditLicensesWindow : Window
        {

        public EditLicensesWindow()
            {
            InitializeComponent();
            UpdateLicenseLists();
            ShowDefaultLicense();
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure a default license is selected if necessary
                if (DefaultCheckBox.IsChecked == true && DefaultLicenseComboBox.SelectedItem == null)
                    {
                    throw new Exception("Please either select a default license, or uncheck the check box relating to default licenses.");
                    }

                // Set default license
                SaveDefaultLicense();

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Could Not Save Changes", ex.Message);
                }

            }

        private void ShowDefaultLicense()
            {
            var defaultLicense = State.Settings.DefaultLicense;
            DefaultLicenseComboBox.SelectedItem = defaultLicense;
            DefaultCheckBox.IsChecked = defaultLicense != null;
            }

        private void SaveDefaultLicense()
            {
            License selectedDefault = DefaultCheckBox.IsChecked == true ? (License)DefaultLicenseComboBox.SelectedItem : null;
            State.Settings.DefaultLicense = selectedDefault;
            State.Settings.SaveChanges();
            }

        private void LicensesListView_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            {
            var licenseSelected = LicensesListView.SelectedItems.Count > 0;
            EditLicenseButton.IsEnabled = licenseSelected;
            DeleteLicenseButton.IsEnabled = licenseSelected;
            }

        private void AddLicenseButton_Click(object sender, RoutedEventArgs e)
            {
            var window = new EditLicenseWindow();
            if (window.ShowDialog() == true)
                {
                UpdateLicenseLists();
                }
            }

        private void EditLicenseButton_Click(object sender, RoutedEventArgs e)
            {

            // Get the selected license
            var selectedLicense = (License)LicensesListView.SelectedItem;
            if (selectedLicense != null)
                {

                // Show the window allowing the license to be edited
                var window = new EditLicenseWindow(selectedLicense);
                if (window.ShowDialog() == true)
                    {
                    UpdateLicenseLists();
                    }

                }
            }

        private void DeleteLicenseButton_Click(object sender, RoutedEventArgs e)
            {

            if (Utilities.Confirm("Delete Licenses", "Are you sure you wish to delete the selected licenses?"))
                {
                var deletedDefaultLicense = false;
                foreach (License license in LicensesListView.SelectedItems)
                    {

                    // Is the default license being deleted?
                    if (DefaultLicenseComboBox.SelectedItem == license) deletedDefaultLicense = true;

                    // Are there songs which depend on this license?
                    var songCount = 0;
                    foreach (var song in State.Database.Songs.GetAllSongs())
                        {
                        if (song.License != null && song.License.ID == license.ID) songCount += 1;
                        }

                    // Ask for confirmation, if there are songs that depend on this license
                    var proceed = true;
                    if (songCount > 0)
                        {
                        var message = string.Format("There {0} {1} {2} in the database using the license '{3}'. If you proceed, then those songs will no longer be assigned a license." + Environment.NewLine + Environment.NewLine +
                                                    "Are you sure you wish to delete the license '{4}'?",
                                                    songCount == 1 ? "is" : "are",
                                                    songCount,
                                                    songCount == 1 ? "song" : "songs",
                                                    license.Name,
                                                    license.Name);
                        proceed = Utilities.Confirm("Delete License", message);
                        }

                    // Delete the license
                    if (proceed) State.Database.Licenses.Delete(license);

                    }

                // Update list of licenses
                UpdateLicenseLists();

                // Was the default license deleted?
                if (deletedDefaultLicense)
                    {
                    DefaultCheckBox.IsChecked = false;
                    DefaultLicenseComboBox.SelectedItem = null;
                    SaveDefaultLicense();
                    ShowDefaultLicense();
                    }

                }

            }

        private void UpdateLicenseLists()
            {

            var currentDefault = DefaultLicenseComboBox.SelectedItem;

            // Populate the license lists
            LicensesListView.Items.Clear();
            DefaultLicenseComboBox.Items.Clear();
            var licenses = State.Database.Licenses.GetAllLicenses().OrderBy(license => license.Name);
            foreach (var license in licenses)
                {
                LicensesListView.Items.Add(license);
                DefaultLicenseComboBox.Items.Add(license);
                }

            // Set default back to what it was, if possible
            if (DefaultLicenseComboBox.Items.Contains(currentDefault))
                {
                DefaultLicenseComboBox.SelectedItem = currentDefault;
                }
            
            }

        private void CheckBoxChanged(object sender, RoutedEventArgs e)
            {
            DefaultLicenseComboBox.IsEnabled = DefaultCheckBox.IsChecked == true;
            }

        }

    }
